package com.winchain.chainbill.bill.biz.discount.service.impl;

import com.shell.bee.cfca.CfcaConfiguration;
import com.winchain.chainbill.ChainBillUtil;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.biz.discount.rx.BillAprvSerFeeDebitBatchSignSubscriber;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctAprvPassSubscriber;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctAprvRejectSubscriber;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctSignSubscriber;
import com.winchain.chainbill.bill.biz.discount.rx.BillSerFeeZYBatchSignSubscriber;
import com.winchain.chainbill.bill.biz.discount.service.BillDctAprvService;
import com.winchain.chainbill.bill.biz.send.rx.BatchBillEvent;
import com.winchain.chainbill.bill.biz.send.service.BillTimeoutService;
import com.winchain.chainbill.bill.biz.send.vo.BatchBillStats;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rx.Observable;

@Service
public class BillDctAprvServiceImpl implements BillDctAprvService {
	@Autowired
	private BillTimeoutService billTimeoutService;
	@Autowired
	private BillDctSignSubscriber billDctSignSubscriber;
	@Autowired
	private BillDctAprvPassSubscriber billDctAprvPassSubscriber;
	@Autowired
	private BillDctAprvRejectSubscriber billDctAprvRejectSubscriber;
	@Autowired
	private BillSerFeeZYBatchSignSubscriber billSerFeeZYBatchSignSubscriber;
	@Autowired
	private BillAprvSerFeeDebitBatchSignSubscriber billAprvSerFeeDebitBatchSignSubscriber;

	public BatchBillStats<CbDct> pass(String phoneCode, String location, String[] billIds) {
		List<String> list = new ArrayList();
		List<String> timeoutList = new ArrayList();
		BatchBillStats<CbDct> stats = new BatchBillStats();
		for (String billId : billIds) {
			if (this.billTimeoutService.check(new String[] { billId, "" })) {
				timeoutList.add(billId);
			} else {
				list.add(billId);
			}
		}
		if ((timeoutList != null) && (timeoutList.size() > 0)) {
			reject("����������������������", timeoutList);
		}
		if ((list != null) && (list.size() > 0)) {
			stats = pass(list, phoneCode, location);
		}
		if (timeoutList.size() == billIds.length) {
			stats.setAffectedRows(-1);
		}
		return stats;
	}

	private BatchBillStats<CbDct> pass(List<String> list, String phoneCode, String location) {
		BatchBillStats<CbDct> stats = new BatchBillStats();

		CbOpt opt = new CbOpt();
		opt.setCurState("S020");
		opt.setOptCode("E006");
		opt.setRemark("������������");

		BatchBillEvent<String, CbDct> event = new BatchBillEvent();
		event.setRecs(list);
		event.setStats(stats);
		event.setOperation(opt);
		event.setValue("phoneCode", phoneCode);
		event.setValue("location", location);
		event.setValue("sealCode", CfcaConfiguration.getCfcaBankSealCode());

		Observable<BatchBillEvent<String, CbDct>> observers = Observable.just(event);
		if (ChainBillUtil.signContOnline()) {
			observers.subscribe(this.billDctSignSubscriber);
			observers.subscribe(this.billSerFeeZYBatchSignSubscriber);
			observers.subscribe(this.billAprvSerFeeDebitBatchSignSubscriber);
		}
		observers.subscribe(this.billDctAprvPassSubscriber);

		return stats;
	}

	private BatchBillStats<CbDct> reject(String remark, List<String> list) {
		BatchBillStats<CbDct> stats = new BatchBillStats();

		BatchBillEvent<String, CbDct> event = new BatchBillEvent();
		event.setRecs(list);
		event.setStats(stats);

		event.setValue("remark", remark);

		Observable<BatchBillEvent<String, CbDct>> observers = Observable.just(event);

		observers.subscribe(this.billDctAprvRejectSubscriber);
		return stats;
	}

	public BatchBillStats<CbDct> reject(String remark, String[] billIds) {
		List<String> list = new ArrayList();
		for (String billId : billIds) {
			list.add(billId);
		}
		return reject(remark, list);
	}
}
